//
//  MOMyGetMarriedPlanEditOrCreatViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/8/16.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOMyGetMarriedPlanEditOrCreatViewController.h"
#import "MOMyGetMarriedProjectEditOrCreatViewCell.h"
#import "MOMyGetMarriedPlanEditOrCreatViewCell.h"
#import "JZGCarSaleImageCellModel.h"
#import "TZImagePickerController.h"
#import "JZGCarSaleImageCellFrame.h"
#import "JZGCarSaleImageCell.h"
#import <Photos/Photos.h>
#import <MobileCoreServices/MobileCoreServices.h>

@interface MOMyGetMarriedPlanEditOrCreatViewController ()<UITableViewDelegate,UITableViewDataSource,JZGCarSaleImageCellDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,TZImagePickerControllerDelegate,UIActionSheetDelegate>

@property(nonatomic,strong)UITableView *tableView;

@property(nonatomic,strong)NSMutableArray *dataSource;

@property(nonatomic,strong)UIView *bottomView;

@property (nonatomic, strong)JZGCarSaleImageCellFrame *saleImageCellFrame;

@property (nonatomic, strong)JZGCarSaleImageCellModel *saleImageCellModel;

@property (nonatomic, strong)NSMutableArray *photoImageArray;

@property(nonatomic,strong)NSDictionary *plainDic;

@end

@implementation MOMyGetMarriedPlanEditOrCreatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
    
    [self initData];
    
    [self getData];
}

-(void)creatView
{
    self.title = @"创建结亲计划";
    
    self.view.backgroundColor = KGrayColor_ededed;
    
    [self.view addSubview:self.tableView];
    
    [self.view addSubview:self.bottomView];
    
    [self setupImageCellData:self.photoImageArray];
}

-(void)initData
{
    self.dataSource = [NSMutableArray array];
    NSArray *arr = @[@"是否特殊人员",@"用户职位"];
    NSArray *pArr = @[@"请选择",@"请填写用户职位"];
    for (int i = 0; i<arr.count; i++) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = [[MOMyGetMarriedProjectEditOrCreatViewModel alloc]init];
        model.nameStr = arr[i];
        model.placeStr = pArr[i];
        model.KeyboardType = UIKeyboardTypeDefault;
        if (i==0) {
            model.layoutType = @"3";
            model.labelWidth = 100;
        }else{
            model.layoutType = @"1";
            model.labelWidth = 80;
        }
        [self.dataSource addObject:@[model]];
    }
    
    NSArray *tArr = @[
                      @[@"来往计划描述，最多150字",@"思想沟通计划描述，最多150字",@"扶帮计划描述，最多150字"],
                      @[@"遇难事、大病必帮描述，最多150字",@"遇喜事、节庆必贺描述，最多150字",@"遇丧事必慰问描述，最多150字"],
                      @[@"结对认亲户家庭困难及需求描述，最多150字",@"帮扶情况描述，最多150字"],
                      ];
    for (int i = 0; i<tArr.count; i++) {
        NSMutableArray *muArr = [NSMutableArray array];
        NSArray *subArr = tArr[i];
        for (int j = 0; j<subArr.count; j++) {
            MOMyGetMarriedPlanEditOrCreatModel *model = [[MOMyGetMarriedPlanEditOrCreatModel alloc]init];
            model.placeStr = subArr[j];
            [muArr addObject:model];
        }
        [self.dataSource addObject:muArr];
    }
    
    [self.dataSource addObject:@[@""]];
    
    [self.tableView reloadData];
}

-(void)getData
{
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTPROJECT_GETECIPIENTPROJECTBYMASTERID_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:@{@"masterId":self.masterId} success:^(id responseObject) {
        NSLog(@"结亲计划查询：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            self.plainDic = responseObject;
            
            for (int i = 0; i<self.dataSource.count; i++) {
                if (i<2) {
                    NSArray *subArr = self.dataSource[i];
                    for (int j = 0; j<subArr.count; j++) {
                        MOMyGetMarriedProjectEditOrCreatViewModel *model = subArr[j];
                        if (i==0) {
                            model.valueStr = [NSString passNullForString:responseObject[@"special"]];
                        }else if(i==1){
                            model.valueStr = [NSString passNullForString:responseObject[@"userDuty"]];
                        }
                    }
                }else{
                    NSArray *subArr = self.dataSource[i];
                    if (i==2) {
                        for (int j = 0; j<subArr.count; j++) {
                            MOMyGetMarriedPlanEditOrCreatModel *model = subArr[j];
                            if (j==0) {
                                model.contentStr = [NSString passNullForString:responseObject[@"comeProject"]];
                            }else if(j==1){
                                model.contentStr = [NSString passNullForString:responseObject[@"exchangeProject"]];
                            }else if(j==2){
                                model.contentStr = [NSString passNullForString:responseObject[@"helpProject"]];
                            }
                        }
                    }else if (i==3){
                        for (int j = 0; j<subArr.count; j++) {
                            MOMyGetMarriedPlanEditOrCreatModel *model = subArr[j];
                            if (j==0) {
                                model.contentStr = [NSString passNullForString:responseObject[@"help"]];
                            }else if(j==1){
                                model.contentStr = [NSString passNullForString:responseObject[@"congratulate"]];
                            }else if(j==2){
                                model.contentStr = [NSString passNullForString:responseObject[@"condole"]];
                            }
                        }
                    }else if (i==4){
                        for (int j = 0; j<subArr.count; j++) {
                            MOMyGetMarriedPlanEditOrCreatModel *model = subArr[j];
                            if (j==0) {
                                model.contentStr = [NSString passNullForString:responseObject[@"need"]];
                            }else if(j==1){
                                model.contentStr = [NSString passNullForString:responseObject[@"helpCase"]];
                            }
                        }
                    }
                    
                }
            }
            
            if ([[NSString passNullForString:responseObject[@"family"]] rangeOfString:@".jpg"].location!=NSNotFound||[[NSString passNullForString:responseObject[@"family"]] rangeOfString:@".png"].location!=NSNotFound) {
                
                NSString *str = [NSString stringWithFormat:@"%@?imgPath=%@",RECIPIENTRECORD_IOREADIMAGE_URL,[NSString URLEncodedString:responseObject[@"family"]]];
                
                [PPNetworkHelper GET:str parameters:@{} success:^(id responseObject) {
                    UIImage *image = [UIImage imageWithData:responseObject];
                    CGSize size = image.size;
                    if (size.width==0) {
                        return ;
                    }
                    NSDictionary *dic = @{
                                          @"image":image,
                                          @"imageType":@"jpg",
                                          @"data":responseObject
                                          };
                    [self.photoImageArray addObject:dic];
                    [self setupImageCellData:self.photoImageArray];
                    [self.tableView reloadData];
                    
                } failure:^(NSError *error) {
                    
                }];
                
            }
            
            [self.tableView reloadData];
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲计划查询：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
}

#pragma mark -- 提交
-(void)clickSubmitButton
{
    for (int i = 0; i<self.dataSource.count; i++) {
        if (i<2) {
            NSArray *arr = self.dataSource[i];
            for (int j = 0; j<arr.count; j++) {
                MOMyGetMarriedProjectEditOrCreatViewModel *model = arr[j];
                if (i==0) {
                    if (model.valueStr.length==0) {
                        [[ADShowMessage shareShowMessage]showTextOnly:@"是否特殊人员不能为空" showInView:self.view];
                        return;
                    }
                }else{
                    if (model.valueStr.length==0) {
                        [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                        return;
                    }
                }
            }
        }
        /*
        else if (i<5){
            NSArray *subArr = self.dataSource[i];
            for (int j = 0; j<subArr.count; j++) {
                MOMyGetMarriedPlanEditOrCreatModel *model = subArr[j];
                if (i==2) {
                    if (j==0) {
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"是否特殊人员不能为空" showInView:self.view];
                            return;
                        }
                    }else if(j==1){
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                            return;
                        }
                    }else if(j==2){
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                            return;
                        }
                    }
                }else if(i==3){
                    if (j==0) {
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"是否特殊人员不能为空" showInView:self.view];
                            return;
                        }
                    }else if(j==1){
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                            return;
                        }
                    }else if(j==2){
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                            return;
                        }
                    }
                }else if(i==4){
                    if (j==0) {
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"是否特殊人员不能为空" showInView:self.view];
                            return;
                        }
                    }else if(j==1){
                        if (model.contentStr.length==0) {
                            [[ADShowMessage shareShowMessage]showTextOnly:@"用户职位不能为空" showInView:self.view];
                            return;
                        }
                    }
                }
            }
        }
         */
    }
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.plainDic.allKeys.count>0) {//修改
        [dic setObject:self.plainDic[@"id"] forKey:@"id"];
    }
    MOMyGetMarriedProjectEditOrCreatViewModel *model1 = self.dataSource[0][0];
    MOMyGetMarriedProjectEditOrCreatViewModel *model2 = self.dataSource[1][0];
    
    MOMyGetMarriedPlanEditOrCreatModel *model3 = self.dataSource[2][0];
    MOMyGetMarriedPlanEditOrCreatModel *model4 = self.dataSource[2][1];
    MOMyGetMarriedPlanEditOrCreatModel *model5 = self.dataSource[2][2];
    
    MOMyGetMarriedPlanEditOrCreatModel *model6 = self.dataSource[3][0];
    MOMyGetMarriedPlanEditOrCreatModel *model7 = self.dataSource[3][1];
    MOMyGetMarriedPlanEditOrCreatModel *model8 = self.dataSource[3][2];
    
    MOMyGetMarriedPlanEditOrCreatModel *model9 = self.dataSource[4][0];
    MOMyGetMarriedPlanEditOrCreatModel *model10 = self.dataSource[4][1];
    
    [dic setObject:[MOUser sharedMOUser].userName forKey:@"userId"];
    [dic setObject:self.masterId forKey:@"masterId"];
    [dic setObject:model2.valueStr forKey:@"userDuty"];
    [dic setObject:model1.valueStr forKey:@"special"];
    [dic setObject:[NSString passNullForString:model3.contentStr] forKey:@"comeProject"];
    [dic setObject:[NSString passNullForString:model4.contentStr] forKey:@"exchangeProject"];
    [dic setObject:[NSString passNullForString:model5.contentStr] forKey:@"helpProject"];
    [dic setObject:[NSString passNullForString:model6.contentStr] forKey:@"help"];
    [dic setObject:[NSString passNullForString:model7.contentStr] forKey:@"congratulate"];
    [dic setObject:[NSString passNullForString:model8.contentStr] forKey:@"condole"];
    [dic setObject:[NSString passNullForString:model9.contentStr] forKey:@"need"];
    [dic setObject:[NSString passNullForString:model10.contentStr] forKey:@"helpCase"];
    
    NSString *imageName1 = @"";
    NSMutableArray *imageArr = [NSMutableArray array];
    NSMutableArray *imageNArr = [NSMutableArray array];
    if (self.photoImageArray.count>0) {
        [imageArr addObject:self.photoImageArray[0]];
        imageName1 = [NSString stringWithFormat:@"%@%@%@_%@",@"family",[self getNowTimeTimestamp3],@"1",@"quanjiafu.jpg"];
        [imageNArr addObject:[imageName1 stringByReplacingOccurrencesOfString:@".jpg" withString:@""]];
    }
    
    //提交表单数据
    NSString *url = [NSString stringWithFormat:@"%@?authorization=%@",RECIPIENTPROJECT_ADDORUPDATEPROJECT_URL,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    if (imageName1.length>0) {
        [dic setObject:imageName1 forKey:@"family"];
    }else{
        [dic setObject:@"" forKey:@"family"];
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper POST:url parameters:dic success:^(id responseObject) {
        NSLog(@"结亲计划创建：%@",responseObject);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"flag"] integerValue]>0) {
            [self.navigationController popViewControllerAnimated:YES];
            [[NSNotificationCenter defaultCenter]postNotificationName:@"hisAction" object:nil];;
        }
    } failure:^(NSError *error) {
        NSLog(@"结亲记录创建：%@",[error localizedDescription]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [[ADShowMessage shareShowMessage]showTextOnly:[error localizedDescription] showInView:self.view];
    }];
    
    //上传图片
    if (imageArr.count==0) {
        return;
    }
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [PPNetworkHelper uploadImagesWithURL:RECIPIENTRECORD_UPLOADIMG_URL parameters:@{} name:imageNArr images:imageArr fileNames:imageNArr imageScale:0.5 imageType:nil progress:^(NSProgress *progress) {
        
    } success:^(id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"%@",responseObject);
    } failure:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
    
}
-(NSString *)getNowTimeTimestamp3{
    
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init] ;
    
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss SSS"]; // ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
    
    //设置时区,这个对于时间的处理有时很重要
    
    NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    
    [formatter setTimeZone:timeZone];
    
    NSDate *datenow = [NSDate date];//现在时间,你可以输出来看下是什么格式
    
    NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[datenow timeIntervalSince1970]*1000];
    
    return timeSp;
}


- (void)setupImageCellData:(NSMutableArray *)imageArr
{
    NSMutableArray *imageA = [NSMutableArray array];
    for (int i = 0; i<imageArr.count; i++) {
        if ([imageArr[i] isKindOfClass:[UIImage class]]) {
            [imageA addObject:imageArr[i]];
        }else if ([imageArr[i] isKindOfClass:[NSString class]]){
            [imageA addObject:imageArr[i]];
        }
        else{
            [imageA addObject:imageArr[i][@"image"]];
        }
    }
    imageArr = imageA;
    
    JZGCarSaleImageCellModel *saleImageCellModel = [[JZGCarSaleImageCellModel alloc]init];
    saleImageCellModel.maxWidth = SCREEN_WIDTH - 24;
    saleImageCellModel.imageArray = imageArr;
    self.saleImageCellModel = saleImageCellModel;
    
    JZGCarSaleImageCellFrame *saleImageCellFrame = [[JZGCarSaleImageCellFrame alloc]init];
    saleImageCellFrame.totalCount = 1;
    saleImageCellFrame.cellModel = saleImageCellModel;
    self.saleImageCellFrame = saleImageCellFrame;
}

#pragma mark - JZGCarSaleImageCellDelegate
- (void)carSaleImageCellDidClickImageButtonAndCellTag:(NSInteger)cellTag
{
    UIActionSheet *imgAction = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册获取", nil];
    [imgAction showInView:self.view];
}

#pragma mark - UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)
    {
        [self showCamera];
        
    }else if (buttonIndex==1)
    {
        [self showImagePick];
    }
}

//打开第三方相册
- (void)showImagePick
{
    if (![self judgeIsHavePhotoAblumAuthority]) {
        NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
        if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
        NSString *msgStr = [NSString stringWithFormat:@"上传头像服务需要访问您的相册，请在%@的\"设置-隐私-照片\"选项中，\r允许%@访问您的手机相册。",[UIDevice currentDevice].model,appName];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相册" message:msgStr preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    
    
    TZImagePickerController *imagePickerVc = nil;
    imagePickerVc = [[TZImagePickerController alloc] initWithMaxImagesCount:1-self.photoImageArray.count delegate:self];
    imagePickerVc.isSelectOriginalPhoto = YES;
    // 1.如果你需要将拍照按钮放在外面，不要传这个参数
    imagePickerVc.allowTakePicture = NO; // 在内部显示拍照按钮
    imagePickerVc.allowPickingVideo = NO;
    imagePickerVc.allowPickingImage = YES;
    imagePickerVc.allowPickingOriginalPhoto = NO;
    imagePickerVc.naviTitleColor = KBlackColor_222222;
    imagePickerVc.barItemTextColor = KBlackColor_222222;
    imagePickerVc.sortAscendingByModificationDate = YES;
    [imagePickerVc setDidFinishPickingPhotosHandle:^(NSArray<UIImage *> *photos, NSArray *assets, BOOL isSelectOriginalPhoto) {
        
    }];
    [self presentViewController:imagePickerVc animated:YES completion:nil];
    
}

-(void)showCamera
{
    if (![self judgeIsHaveCameraAuthority]) {
        NSString *appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleDisplayName"];
        if (!appName) appName = [[NSBundle mainBundle].infoDictionary valueForKey:@"CFBundleName"];
        NSString *msgStr = [NSString stringWithFormat:@"上传图片服务需要访问您的相机，请在%@的\"设置-隐私\"选项中，\r允许%@访问您的手机相机。",[UIDevice currentDevice].model,appName];
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"无法访问相机" message:msgStr preferredStyle:UIAlertControllerStyleAlert];
        
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil]];
        [self presentViewController:alertController animated:YES completion:nil];
        return;
    }
    
    //创建UIImagePickerController对象，并设置代理和可编辑
    UIImagePickerController * imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.editing = YES;
    imagePicker.delegate = self;
    imagePicker.allowsEditing = YES;
    
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceFront] || [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear]) {
        //选择相机时，设置UIImagePickerController对象相关属性
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        imagePicker.modalPresentationStyle = UIModalPresentationFullScreen;
        imagePicker.mediaTypes = @[(NSString *)kUTTypeImage];
        imagePicker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        //跳转到UIImagePickerController控制器弹出相机
        [self presentViewController:imagePicker animated:YES completion:nil];
    }else{
        [[ADShowMessage shareShowMessage]showTextOnly:@"当前设备不支持拍摄" showInView:self.view];
    }
}

- (void)imagePickerController:(TZImagePickerController *)picker didFinishPickingPhotos:(NSArray *)photos sourceAssets:(NSArray *)assets isSelectOriginalPhoto:(BOOL)isSelectOriginalPhoto {
    [picker dismissViewControllerAnimated:YES completion:nil];

    for (int i = 0; i<photos.count; i++) {
        NSDictionary *dic = @{
                              @"image":photos[i],
                              @"imageType":@"jpg",
                              @"data":UIImageJPEGRepresentation(photos[i], 0.5f)
                              };
        [self.photoImageArray addObject:dic];
    }
    [self setupImageCellData:self.photoImageArray];
    
    //        NSIndexPath *indexPathA = [NSIndexPath indexPathForRow:0 inSection:0];
    //        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathA,nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadData];
        
   
}

#pragma mark - imagePickerController delegate
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    
    UIImage *pickImage = [info objectForKey:UIImagePickerControllerEditedImage];
    NSDictionary *dic = @{
                          @"image":pickImage,
                          @"imageType":@"jpg",
                          @"data":UIImageJPEGRepresentation(pickImage,0.5f)
                          };
    
    [self.photoImageArray addObject:dic];
    [self setupImageCellData:self.photoImageArray];
    //        NSIndexPath *indexPathA = [NSIndexPath indexPathForRow:0 inSection:0];
    //        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathA,nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadData];
        
    
}

//按取消按钮时候的功能
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [picker dismissViewControllerAnimated:YES completion:nil];
}

#pragma mark - 判断软件是否有相册、相机访问权限
- (BOOL)judgeIsHavePhotoAblumAuthority
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted ||
        status == PHAuthorizationStatusDenied) {
        return NO;
    }
    return YES;
}

- (BOOL)judgeIsHaveCameraAuthority
{
    AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
    if (status == AVAuthorizationStatusRestricted ||
        status == AVAuthorizationStatusDenied) {
        return NO;
    }
    return YES;
}

#pragma mark - 删除图片
-(void)carSaleImageCellDelegateDidDeleteImageWithIndex:(NSInteger)imageIndex andCellTag:(NSInteger)cellTag
{
    
    [self.photoImageArray removeObjectAtIndex:imageIndex];
    [self setupImageCellData:self.photoImageArray];
    
    //        NSIndexPath *indexPathA = [NSIndexPath indexPathForRow:0 inSection:0];
    //        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPathA,nil] withRowAnimation:UITableViewRowAnimationAutomatic];
    [self.tableView reloadData];
    
}

- (void)carSaleImageCellDidClickImageButton {
    
}


#pragma mark -- UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSArray *arr = self.dataSource[section];
    return arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0||indexPath.section==1) {
        MOMyGetMarriedProjectEditOrCreatViewCell * cell = [MOMyGetMarriedProjectEditOrCreatViewCell cellWithTableView:tableView andIndexPath:indexPath];
        cell.model = self.dataSource[indexPath.section][indexPath.row];
        
        return cell;
    }else if (indexPath.section==5){
        JZGCarSaleImageCell *imageCell = [JZGCarSaleImageCell cellWithTableView:tableView];
        imageCell.allPhotoNumber = 1;
        imageCell.cellFrame = self.saleImageCellFrame;
        imageCell.delegate = self;
        imageCell.backgroundColor = KWhiteColor;
        return imageCell;
    }else{
        MOMyGetMarriedPlanEditOrCreatViewCell *cell = [MOMyGetMarriedPlanEditOrCreatViewCell cellWithTableView:tableView andIndexPath:indexPath];
        MOMyGetMarriedPlanEditOrCreatModel *model = self.dataSource[indexPath.section][indexPath.row];
        cell.textViewEndEditBlock = ^(NSString * _Nonnull text) {
            model.contentStr = text;
        };
        cell.model = model;
        return cell;
    }
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0||indexPath.section==1) {
        return kHConversions(54);
    }else if (indexPath.section==5){
        return self.saleImageCellFrame.imageContainerViewFrame.size.height + 20;
    }else{
        return 140;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section==5) {
        return 10;
    }else{
        return 0.0001;
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0||section==4) {
        return 10;
    }else if (section==1){
        return 1;
    }else if (section==2||section==3){
        return kHConversions(54);
    }else{
        return 10 + kHConversions(54);
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section==0||section==1||section==4) {
        return nil;
    }else if (section==2||section==3){
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54))];
        
        NSArray *arr = @[@"“三定”计划",@"“两帮两贺一慰问”情况"];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH, backView.height - 10)];
        label.text = arr[section-2];
        label.textColor = KBlackColor_222222;
        label.font = [UIFont boldSystemFontOfSize:15];
        [backView addSubview:label];
        
        return backView;
    }else{
        UIView *backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, kHConversions(54) + 10)];
        
        UIView *subView = [[UIView alloc]initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, kHConversions(54))];
        subView.backgroundColor = KWhiteColor;
        [backView addSubview:subView];
        
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(15, 0, SCREEN_WIDTH, kHConversions(54))];
        label.text = @"全家福";
        label.textColor = KBlackColor_222222;
        label.font = [UIFont boldSystemFontOfSize:15];
        [subView addSubview:label];
        
        return backView;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return nil;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        MOMyGetMarriedProjectEditOrCreatViewModel *model = self.dataSource[indexPath.section][indexPath.row];
        SNXYSelectedNumberView *pickview = [[SNXYSelectedNumberView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        NSArray *tArr = @[@"是",@"否"];
        pickview.numberArray = tArr;
        __weak typeof (self)weakSelf = self;
        pickview.clickIndexBlock = ^(NSInteger index) {
            model.valueStr = tArr[index];
            [weakSelf.tableView reloadData];
        };
        [WINDOW addSubview:pickview];
    }
}


#pragma mark -懒加载
-(UITableView *)tableView
{
    if (!_tableView) {
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-StatusBarAndNavigationBarHeight - 57 - TabbarSafeBottomMargin) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = KGrayColor_ededed;
        _tableView.showsVerticalScrollIndicator = NO;
        
    }
    return _tableView;
}

-(UIView *)bottomView
{
    if (!_bottomView) {
        _bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - StatusBarAndNavigationBarHeight - TabbarSafeBottomMargin - 57, SCREEN_WIDTH, 57)];
        _bottomView.backgroundColor = KWhiteColor;
        
        UIButton *submitButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, _bottomView.width, _bottomView.height)];
        [submitButton setTitle:@"提交" forState:UIControlStateNormal];
        [submitButton setTitleColor:KBlueColor_2C94FF forState:UIControlStateNormal];
        submitButton.titleLabel.font = FONT_SIZE(15);
        [submitButton addTarget:self action:@selector(clickSubmitButton) forControlEvents:UIControlEventTouchUpInside];
        [_bottomView addSubview:submitButton];
        
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 1)];
        lineView.backgroundColor = KGrayColor_ededed;
        [_bottomView addSubview:lineView];
    }
    return _bottomView;
}

-(NSMutableArray *)photoImageArray
{
    if (!_photoImageArray) {
        _photoImageArray = [NSMutableArray array];
    }
    return _photoImageArray;
}

@end
